package com.ita.osc.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ita.osc.bean.Cart;
import com.ita.osc.bean.Dish;
import com.ita.osc.bean.Order;
import com.ita.osc.dao.OrderDao;
import com.ita.osc.db.DbConnection;

public class OrderDaoImpl implements OrderDao {

	@Override
	public List<Order> listOrderByUId(String cuid) {
		// TODO Auto-generated method stub
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM corder where cuid = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, cuid);
			rs = pst.executeQuery();
			Order order=null;
			while (rs.next()) {
				String oid = rs.getString("oid");
				Date createDate = rs.getDate("createdate");
				double total = rs.getDouble("total");
				order = new Order(oid, cuid, createDate, total);
				list.add(order);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public int addOrder(Order order) {
		// TODO Auto-generated method stub
		String sql = "insert into corder (oid, cuid, createdate, total) values (?, ?, ?, ?)";
		Connection con = null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, order.getOid());
			pst.setString(2, order.getUid());
			pst.setDate(3, new java.sql.Date( order.getCreateDate().getTime()));
			pst.setDouble(4, order.getTotal());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return m;
	}

	@Override
	public void insertRelationTable(String oid, Map<Dish, Integer> cartlist) {
		// TODO Auto-generated method stub
		String sql = "insert into ORDER_DISH (oid, did, quantity) values (?, ?, ?)";
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			for (Dish dish : cartlist.keySet()) {
				pst = con.prepareStatement(sql);
				pst.setString(1, oid);
				pst.setString(2, dish.getDid());
				pst.setInt(3, cartlist.get(dish));
				pst.executeUpdate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public Map<Dish, Integer> listDishByOrderId(String oid) {
		// TODO Auto-generated method stub
		Map<Dish, Integer> cartList = new HashMap<Dish, Integer>();
		String sql = "SELECT * FROM ORDER_DISH left join dish on ORDER_DISH.did = dish.did where ORDER_DISH.oid = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, oid);
			rs = pst.executeQuery();
			Dish dish=null;
			while (rs.next()) {
				String did = rs.getString("did");
				String dname = rs.getString("dname");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				dish = new Dish(did, dname, price, img);
				int quantity = rs.getInt("quantity");
				cartList.put(dish, quantity);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cartList;
	}

}
